function $C_(name) {
	return document.getElementsByName(name);
}
function $C_id(id) {
	return document.getElementById(id);
}
function _ajax(url, operate, params) {
	document.getElementById("lodingDiv").style.height = document.body.clientHeight
			- 3 + "px";
	document.getElementById("lodingDiv").style.display = '';
	var xmlhttp = null;
	if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera,
		xmlhttp = new XMLHttpRequest();
	} else {// code for IE6, IE5
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange = function() {
		if (xmlhttp.readyState == 4) {
			if (xmlhttp.status == 200) {
				text = xmlhttp.responseText;
				if(text&&text!=""){
					operate(eval("(" + text + ")"));
				}
			} else {
				alert("连接服务器异常");
			}
			document.getElementById("lodingDiv").style.display = 'none';
		}
	};
	xmlhttp.open("post", url, true);
	xmlhttp.setRequestHeader("Content-Type",
			"application/x-www-form-urlencoded");
	xmlhttp.send(params + "&temp=" + Math.random());
}
function createIndex(name) {
	for ( var i = 0; i < $C_(name).length; i++) {
		$C_(name)[i].innerHTML = i + 1;
	}
}
function addSQLTR(input) {
	var trNode = input.parentNode.parentNode.parentNode.insertBefore(
			input.parentNode.parentNode.cloneNode(true), document
					.getElementById("errorTR"));
	trNode.childNodes[1].childNodes[0].value = "";

}
function delSQLTR(input) {
	if ($C_("sql").length > 1) {
		input.parentNode.parentNode.parentNode
				.removeChild(input.parentNode.parentNode);
	} else {
		$C_("sql")[0].value = "";
	}
}
/**
 * 显示具体ＳＱＬ项
 * 
 * @param {}
 *            SQLArray
 */
function showSQLTR(SQLArray) {
	if (SQLArray) {
		var oldLength = $C_("sql").length;
		var newLength = SQLArray.length;
		var count = newLength - oldLength;
		if (count != 0) {
			for ( var i = 0; i < Math.abs(count); i++) {
				if (count > 0)
					addSQLTR($C_("sql")[0]);
				if (count < 0)
					delSQLTR($C_("sql")[0]);
			}
		}
		for ( var i = 0; i < newLength; i++) {
			$C_("sql")[i].value = SQLArray[i];
		}
	} else {
		var oldLength = $C_("sql").length;
		for ( var i = 0; i < oldLength; i++) {
			delSQLTR($C_("sql")[0]);
		}
	}
	createIndex("sqlIndex");
}

function showGridTable(rows) {
	if (rows && rows.length > 0) {
		$C_("gridIndex")[0].parentNode.parentNode.style.display = "";
	} else {
		$C_("gridIndex")[0].parentNode.parentNode.style.display = "none";
	}
	var count = (rows ? rows.length : 0) - $C_("gridIndex").length;
	for ( var i = 0; i < Math.abs(count); i++) {
		if (count > 0) {
			$C_("gridIndex")[0].parentNode.parentNode.parentNode.insertBefore(
					$C_("gridIndex")[0].parentNode.parentNode.cloneNode(true),
					$C_("gridIndex")[0].parentNode.parentNode);
		} else {
			if ($C_("gridIndex").length > 1)
				$C_("gridIndex")[0].parentNode.parentNode.parentNode
						.removeChild($C_("gridIndex")[$C_("gridIndex").length - 1].parentNode.parentNode);
			else {
				$C_("gridIndex")[0].parentNode.parentNode.style.display = "none";
			}
		}
	}
	createIndex("gridIndex");
	if (rows) {
		for ( var i = 0; i < rows.length; i++) {
			$C_("grid_name")[i].value = rows[i].name;
			$C_("grid_description")[i].value = rows[i].description;
		}
	}
}
function querySQLSet() {
	_ajax("../common.do", function(data) {
		if (data.success) {
			returnButtonClick();
			showGridTable(data.rows);
		} else {
			alert(data.msg);
		}
	}, "method=commQuerySet&operate=query");
}
function clearSQLSet() {
	$C_("name")[0].value = "";
	$C_("description")[0].value = "";
	$C_("database")[0].value = "";
	$C_("errorMsg")[0].value = "";
	$C_("nullMsg")[0].value = "";
	$C_("fieldName")[0].value = "";
	showSQLTR([]);
}
function returnButtonClick() {
	$C_id("showSQL").style.display = "none";
	$C_id("gridTable").style.display = "";
	$C_("name").readOnly = true;
	clearSQLSet();
}
function addSQLSet() {
	$C_id("showSQL").style.display = "";
	$C_id("gridTable").style.display = "none";
	$C_("name")[0].readOnly = false;
	clearSQLSet();
}

function save() {
	var url = "../common.do"
	var params = "method=commQuerySet";
	params = params + "&name=" + encodeURIComponent($C_("name")[0].value)
			+ "&database=" + encodeURIComponent($C_("database")[0].value);
	params = params + "&errorMsg=" + encodeURIComponent($C_("errorMsg")[0].value)
			+ "&nullMsg=" + encodeURIComponent($C_("nullMsg")[0].value)
			+ "&fieldName=" + encodeURIComponent($C_("fieldName")[0].value);
	params = params + "&description="
			+ encodeURIComponent($C_("description")[0].value);
	for ( var i = 0; i < $C_("sql").length; i++) {
		params = params + "&sql=" + encodeURIComponent($C_("sql")[i].value);
	}
	if ($C_("name")[0].readOnly == false) {
		params = params + "&operate=add";
	} else {
		params = params + "&operate=change";
	}
	_ajax(url, function(data) {
		if (data.success) {
			alert($C_("name")[0].readOnly ? "已更新" : "已新增");
			querySQLSet();
		} else {
			alert(data.msg);
		}
	}, params);

}
function changeSQLSet(button) {
	$C_id("showSQL").style.display = "";
	$C_id("gridTable").style.display = "none";
	$C_("name")[0].readOnly = true;
	_ajax("../common.do", function(data) {
		if (data.success) {
			var sqlSet = data.sqlSet;
			$C_("name")[0].value = sqlSet.name;
			$C_("description")[0].value = sqlSet.description;
			$C_("database")[0].value = sqlSet.database;
			$C_("errorMsg")[0].value = sqlSet.errorMsg;
			$C_("nullMsg")[0].value = sqlSet.nullMsg;
			$C_("fieldName")[0].value = sqlSet.fieldName;
			showSQLTR(sqlSet.sql)
		} else {
			alert(data.msg);
		}
	}, "method=commQuerySet&operate=look&name="
			+ button.parentNode.parentNode.getElementsByTagName("INPUT")[0].value);
}
function delSQLSet(button) {
	if (confirm("请确认要删除名称为"
			+ button.parentNode.parentNode.childNodes[1].childNodes[0].value
			+ "的ＳＱＬ设置")) {
		_ajax(
				"../common.do",
				function(data) {
					if (data.success) {
						alert("已删除");
						querySQLSet();
					} else {
						alert(data.msg);
					}
				},
				"method=commQuerySet&operate=del&name="
						+ button.parentNode.parentNode.childNodes[1].childNodes[0].value);

	}
}

window.onload = function() {
	querySQLSet();
};